package com.kucunProject.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.hibernate.id.UUIDHexGenerator;

import com.kucunProject.dao.DBManager;
import com.kucunProject.model.CategoryInfo;

public class CategoryInfoService {

	public boolean addInfo(int productClassfy, String ciName, String remark) {

		boolean flag = true;

		DBManager dbm = new DBManager();
		String sql = "insert into tb_kc_category_info values (?,?,?,sysdate,?,?)";
		PreparedStatement ps = null;
		String id = getId();

		try {
			ps = dbm.connect().prepareStatement(sql);
			ps.setString(1, id);
			ps.setInt(2, productClassfy);
			ps.setString(3, ciName);
			ps.setString(4, remark);
			ps.setInt(5, 0);
			flag = ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return flag;

	}

	public boolean deleteInfo(String id) {
		boolean flag = true;

		DBManager dbm = new DBManager();
		StringBuffer sql = new StringBuffer();
		PreparedStatement ps = null;
		sql.append("delete from tb_kc_category_info where 1=1");

		try {
			if (id != null && !"".equals(id)) {
				sql.append("and ci_id = '").append(id).append("'");
				ps = dbm.connect().prepareStatement(sql.toString());
				flag = ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return flag;
	}

	public boolean updateInfo(String ciId, String ciName, int productClassfy,
			String remark) {

		boolean flag = true;

		DBManager dbm = new DBManager();
		String sql = "update tb_kc_category_info set ci_name = ?,product_classfy = ?,remark = ? where ci_id = ?";
		PreparedStatement ps = null;

		try {
			ps = dbm.connect().prepareStatement(sql);
			ps.setString(1, ciName);
			ps.setInt(2, productClassfy);
			ps.setString(3, remark);
			ps.setString(4, ciId);
			flag = ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return flag;

	}

	public ArrayList<CategoryInfo> selectInfo(String name) {

		ArrayList<CategoryInfo> list = new ArrayList<CategoryInfo>();

		DBManager dbm = new DBManager();
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append("select * from tb_kc_category_info where 1=1 ");
		if (name != null && !"".equals(name)) {
			sql.append("and ci_name like '%").append(name).append("%'");
		}
		try {
			ps = dbm.connect().prepareStatement(sql.toString());
			rs = ps.executeQuery();
			while (rs.next()) {
				CategoryInfo ci = new CategoryInfo();
				ci.setCiId(rs.getString("CI_ID"));
				ci.setCiName(rs.getString("CI_NAME"));
				ci.setProductClassfy(rs.getInt("PRODUCT_CLASSFY"));
				ci.setCreateDate(rs.getDate("CREATE_DATE"));
				ci.setRemark(rs.getString("REMARK"));
				ci.setIsDelete(rs.getInt("ISDELETE"));
				list.add(ci);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;

	}

	public static String getId() {
		String id = (String) new UUIDHexGenerator().generate(null, null);
		return id;
	}

	public CategoryInfo selectInfo2(String id) {

		CategoryInfo ci = new CategoryInfo();

		DBManager dbm = new DBManager();
		String sql = "select * from tb_kc_category_info where ci_id = ?";
		ResultSet rs = null;
		PreparedStatement ps = null;

		try {
			ps = dbm.connect().prepareStatement(sql);
			ps.setString(1, id);
			rs = ps.executeQuery();
			while (rs.next()) {
				ci.setCiId(id);
				ci.setCiName(rs.getString("CI_NAME"));
				ci.setProductClassfy(rs.getInt("PRODUCT_CLASSFY"));
				ci.setCreateDate(rs.getDate("CREATE_DATE"));
				ci.setRemark(rs.getString("REMARK"));
				ci.setIsDelete(rs.getInt("ISDELETE"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return ci;

	}

}
